<template>
  <div class="updateInfo">

    <!--编辑弹出框-->
    <el-dialog title="修改个人信息" :visible.sync="dialogVisibleforedit" top="5vh" width="30%">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="120px">
        <el-form-item label="教工号" prop="tno">
          <el-input v-model="form.tno" disabled style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="tname">
          <el-input v-model="form.tname" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="tsex">
          <el-radio v-model="form.tsex" label="男"></el-radio>
          <el-radio v-model="form.tsex" label="女"></el-radio>
        </el-form-item>
        <el-form-item label="年龄" prop="tage">
          <el-input v-model="form.tage" clearable style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="学历" prop="teb">
          <el-select v-model="form.teb" style="width: 50%" placeholder="请选择学历">
            <el-option
                v-for="item in tebs"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="职称" prop="tpt">
          <el-select v-model="form.tpt" style="width: 50%" placeholder="请选择职称">
            <el-option
                v-for="item in tpts"
                :label="item.label"
                :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="主讲课程一" prop="cno1">
          <el-input v-model="form.cno1" disabled style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="主讲课程二" prop="cno2">
          <el-input v-model="form.cno2" disabled style="width: 80%"></el-input>
        </el-form-item>
        <el-form-item label="主讲课程三" prop="cno3">
          <el-input v-model="form.cno3" disabled style="width: 80%"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisibleforedit = false">取 消</el-button>
        <el-button type="primary" @click="editSave">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import request2 from "@/utils/request2";

export default {
  name: "UpdateInfo",
  data() {
    const checkAge = (rule, value, callback) => {
      if (!value) {
        callback(new Error('年龄不能为空'))
      }
      if (!/^[0-9]*$/.test(value)) {
        callback(new Error('请输入数字值'));
      }
      if (parseInt(value) < 24 || parseInt(value) > 60) {
        callback(new Error('年龄只能为24-60范围内'));
      }
      callback();
    };
    return {
      dialogVisibleforedit: false,
      tno: JSON.parse(Cookies.get('teacher')).username,
      form: {},
      depts: [],
      tebs: [
        {
          label: '学士',
          value: '学士'
        }, {
          label: '硕士',
          value: '硕士'
        }, {
          label: '博士',
          value: '博士'
        }],     //学历
      tpts: [
        {
          label: '助教',
          value: '助教'
        }, {
          label: '讲师',
          value: '讲师'
        }, {
          label: '副教授',
          value: '副教授'
        }, {
          label: '教授',
          value: '教授'
        }],     //职称
      rules: {
        tname: [
          {required: true, message: '请输入姓名', trigger: 'blur'},
        ],
        tage: [
          {validator: checkAge, trigger: 'blur'},
        ],
      }
    }
  },
  created() {
    this.dialogVisibleforedit = true
    request2.get('/teacherOfTeacher/getTeacherInfo/' + this.tno).then(res => {
      this.form = res.data
    })
  },
  methods: {
    editSave() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          request2.put('/teacherOfTeacher/update/',this.form).then(res => {
            if (res.code == '200') {
              this.$notify.success("编辑成功")
              this.dialogVisibleforedit = false
              this.load()
            } else {
              this.$notify.error('编辑失败')
            }
          })
        }  else {
          this.$notify.warning('请填写全部信息')
        }
      })
    },
  }
}
</script>
